var express = require('express');
var router = express.Router();
var db = require("../util/DataBase.js");

var messageService = require("../service/message.js");

router.get('/inbox/unread', function(req, res, next) {

  var userId = req.session.user.id;

  messageService.unread(userId, (count) => {
    res.json(count);
  }, () => {
    res.json(0);
  });

});

router.post('/have_read', function(req, res, next) {

  var userId = req.session.user.id;

  db.query("UPDATE inbox SET `read` = 1 WHERE `user_id` = ? AND `id` = ?", [
    userId * 1,
    req.body.inbox_id * 1
  ], function (err, results) {
    if(err) {
      res.json(-1, "设置已读失败");
    } else {
      res.json({});
    }
  });

});

router.get('/inbox/question_list', function(req, res, next) {

  var userId = req.session.user.id;

  messageService.getMessageList(userId, "question", req.query.startId * 1, req.query.len * 1, req.query.dir, function (results) {
    if(results.length == 0) {
      res.json([]);
      return;
    }
    var loaded = 0;

    for(var i = 0, l = results.length; i < l; ++ i) {
      (function (_inboxMessage) {

        messageService.getMessageContent(_inboxMessage.message_id, function (messageEntity) {
          _inboxMessage.message_content = messageEntity;
          db.query("SELECT `id`, `nickname`, `avatar_url` FROM `user` WHERE `id` = ?", [_inboxMessage.message_content.user_id], function (err, userResults) {
            _inboxMessage.message_content.user = userResults[0];

            if( ++ loaded >= l ) {
              res.json(results);
            }

          });
        }, function () {
          res.json(-1, "获取消息实体失败");
        });

      })(results[i]);
    }
  }, function () {
    res.json(-1, "获取消息列表失败");
  });

});

router.get('/inbox/answer_list', function(req, res, next) {

  var userId = req.session.user.id;

  messageService.getMessageList(userId, "answer", req.query.startId * 1, req.query.len * 1, req.query.dir, function (results) {
    if(results.length == 0) {
      res.json([]);
      return;
    }
    var loaded = 0;

    for(var i = 0, l = results.length; i < l; ++ i) {
      (function (_inboxMessage) {

        messageService.getMessageContent(_inboxMessage.message_id, function (messageEntity) {
          _inboxMessage.message_content = messageEntity;

          db.query("SELECT `id`, `nickname`, `avatar_url` FROM `user` WHERE `id` = ?", [_inboxMessage.message_content.article.user_id], function (err, userResults) {
            _inboxMessage.message_content.article.user = userResults[0];

            if( ++ loaded >= l ) {
              res.json(results);
            }

          });
        }, function () {
          res.json(-1, "获取消息实体失败");
        });

      })(results[i]);
    }
  }, function () {
    res.json(-1, "获取消息列表失败");
  });

});

router.get('/inbox/reply_list', function(req, res, next) {

  var userId = req.session.user.id;

  messageService.getMessageList(userId, "reply", req.query.startId * 1, req.query.len * 1, req.query.dir, function (results) {
    if(results.length == 0) {
      res.json([]);
      return;
    }
    var loaded = 0;

    for(var i = 0, l = results.length; i < l; ++ i) {
      (function (_inboxMessage) {

        messageService.getMessageContent(_inboxMessage.message_id, function (messageEntity) {
          _inboxMessage.message_content = messageEntity;
          if(_inboxMessage.message_content.replyComment) {
            if( ++ loaded >= l ) {
              res.json(results);
            }
          } else {
            db.query("SELECT `id`, `nickname`, `avatar_url` FROM `user` WHERE `id` = ?", [_inboxMessage.message_content.article.user_id], function (err, userResults) {
              _inboxMessage.message_content.article.user = userResults[0];

              if( ++ loaded >= l ) {
                res.json(results);
              }

            });
          }

        }, function () {
          res.json(-1, "获取消息实体失败");
        });

      })(results[i]);
    }
  }, function () {
    res.json(-1, "获取消息列表失败");
  });

});

module.exports = router;
